package com.myqm.service.impl.weixin;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myqm.dao.weixin.CustomerDao;
import com.myqm.dao.weixin.UserDao;
import com.myqm.pojo.weixin.Customer;
import com.myqm.pojo.weixin.User;
import com.myqm.pojo.weixin.Verified;
import com.myqm.pojo.weixin.query.QueryUser;
import com.myqm.service.weixin.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private CustomerDao customerDao;
	
	@Override
	public int delById(Integer id) {
		return userDao.delById(id);
	}

	@Override
	public int save(User record) {
		return userDao.save(record);
	}

	@Override
	public User getById(Integer id) {
		return userDao.getById(id);
	}


	@Override
	public int edit(User record) {
		return userDao.editById(record);
	}

	@Override
	public PageInfo<User> list(int pagenum, int pagesize,QueryUser param) {
		PageHelper.startPage(pagenum, pagesize);
		List<User> datalist = userDao.list(param); 
		PageInfo<User> p = new PageInfo<User>(datalist);
		return p;
	}

	@Override
	public User getUser() {
		//得到当前用户的id
		int id = 1;
		return userDao.getById(id);
	}

	@Override
	public int verified(Verified record) {		
		userDao.chageVerifiedTime(record.getId(), new Timestamp(new Date().getTime()));	
		if(record.getGsId() == 0 || record.getGsId() == null) {
			return userDao.verified(record.getId(), "2");
		}else {
			//将customer的customer_name，pinyinfieldname放进weixinuser
			Customer customer = customerDao.getById(record.getGsId());
			User user = userDao.getById(record.getId());
			user.setCustomerName(customer.getCustomerName());
			user.setPinyinfieldname(customer.getPinyinfieldname());
			userDao.editById(user);
			return userDao.verified(record.getId(), "1");
		}
	}

	
}
